import commands
from pyspades.server import hit_packet

@commands.name('fighter')
def fighter(connection):
    connection.bandfs = 0
    return 'You are now a Fighter.'
@commands.name('builder')
def builder(connection):
    connection.bandfs = 1
    return 'You are now a Builder.'
commands.add(fighter)
commands.add(builder)

def apply_script(protocol, connection, config):
    
    class ProtectConnection(connection):
        bandfs = 0
        def on_hit(self, hit_amount, hit_player):
            value = connection.on_hit(self, hit_amount, hit_player)
            if value is not None:
                return value
            if self.bandfs == 0 and hit_player.bandfs == 0:
                return hit_amount
            if hit_player.bandfs == 1:
                self.send_chat( ' You cannot hurt a builder! ' )
                return 0
            if self.bandfs == 1:
                self.send_chat( ' Builders cannot hurt other players! ' )
                return 0
        def on_block_destroy(self, x, y, z, value):
            value = connection.on_block_destroy(self, x, y, z, value)
            if value is not None:
                return value
            if self.bandfs == 0:
                self.send_chat( ' Fighters cannot destroy blocks! ' )
                return False
        def on_block_build_attempt(self, x, y, z):
            value = connection.on_block_build_attempt(self, x, y, z)
            if value is not None:
                return value
            if self.bandfs == 0:
                self.send_chat( ' Fighters cannot place blocks! ' )
                return False
    return protocol, ProtectConnection